package com.itheima.reggie.controller;

import com.alibaba.druid.sql.dialect.odps.parser.OdpsExprParser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.ExcelData;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<?> userPage(int page, int pageSize) {
        Orders orders = new Orders();
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, orders.getUserId());
        //根据ID查询菜品名称
        Page<Orders> page1 = orderService.page(pageInfo, queryWrapper);
        return R.success(page1);
    }


    @GetMapping("/page")
    public R<?> page(int page, int pageSize, Long number, String beginTime, String endTime) {

        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //订单号查询
        queryWrapper.like(number != null, Orders::getNumber, number);
        //时间字段查询
        queryWrapper.between(beginTime != null, Orders::getOrderTime, beginTime, endTime);

        orderService.page(pageInfo, queryWrapper);
        //对象拷贝（展示用户名字字段
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");

        List<Orders> ord = pageInfo.getRecords();
        List<OrdersDto> list = ord.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //拷贝
            BeanUtils.copyProperties(item, ordersDto);
            Long userId = item.getAddressBookId();
            AddressBook byId = addressBookService.getById(userId);
            String consignee = byId.getConsignee();
            ordersDto.setUserName(consignee);
            return ordersDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    @PutMapping
    public R<?> put(@RequestBody OrdersDto ordersDto) {
        boolean b = orderService.updateById(ordersDto);
        return R.success(b);
    }


    @PostMapping("/send")
    public R<?> excel(@RequestBody List<Orders> orders) {

        ArrayList<ExcelData> excelData1 = new ArrayList<>();
        for (Orders order : orders) {
            ExcelData excelData = new ExcelData();

            excelData.setNumber(order.getNumber());

            if (order.getStatus() == 1) {
                excelData.setStatus("待付款");
            } else if (order.getStatus() == 2) {
                excelData.setStatus("待派送");
            } else if (order.getStatus() == 3) {
                excelData.setStatus("已派送");
            } else if (order.getStatus() == 4) {
                excelData.setStatus("已完成");
            } else if (order.getStatus() == 5) {
                excelData.setStatus("已取消");
            }
            excelData.setUserName(order.getUserName());
            excelData1.add(excelData);
        }

        String path = "D:\\";
        // 路径+表格名称
        String fileName = path + "EasyExcel.xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        com.alibaba.excel.EasyExcel.write(fileName, ExcelData.class).sheet("订单明细表").doWrite(excelData1);

        return R.success("导出成功");
    }

}


